package cn.jd.test;

import cn.jd.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class EsTestTwo {
    @Test
    public void testAdd(){

        // 获取连接
        TransportClient client = ESClientUtil.getClient();
        // 准备数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",2);
        map.put("name","阮经天");
        map.put("age",22);
        map.put("sex",false);
        // 链式编程添加数据
        IndexResponse response = client.prepareIndex("pethome", "employee", "1").setSource(map).get();
        System.out.println(response);
        client.close();
    }
    @Test
    public void testGet(){
        // 获取连接
        TransportClient client = ESClientUtil.getClient();
        // 链式编程添加数据
        Map<String, Object> source = client.prepareGet("pethome", "employee", "1").get().getSource();
        System.out.println(source);
        client.close();
    }
    // 修改
    @Test
    public void testUpdate(){
        // 获取连接
        TransportClient client = ESClientUtil.getClient();
        // 准备需要修改的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","河北菜花");
        // 链式编程添加数据
        UpdateResponse updateResponse = client.prepareUpdate("pethome", "employee", "1").setDoc(map).get();
        System.out.println(updateResponse);
        client.close();
    }
    // 删除
    @Test
    public void testDel(){
        // 获取连接
        TransportClient client = ESClientUtil.getClient();
        // 链式编程添加数据
        DeleteResponse deleteResponse = client.prepareDelete("pethome", "employee", "1").get();
        System.out.println(deleteResponse);
        client.close();
    }
}
